﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YJKRevitKernel.RevitDimensionUtility;
using YJKRevitDimensionText.Commands;
using YJKRevitDimensionText.Forms;
using YJKRevitDimensionText.Utility;
using YJKRevitTextUtil;

namespace YJKRevitDimension.PracticeDimension
{
	public partial class DlgPracticeDim : System.Windows.Forms.Form
	{
		public DlgPracticeDim(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgPracticeDim_Load(object sender, EventArgs e)
		{
			Helper.AddCombItems(DimDate.ArrowType, this.cmbArrowStyle);
			Helper.AddCombItems(DimDate.FontFamily, this.cboFontType);
			this.ReadDlgData();
			this.SetDlgData();
			if (this.m_nDlgPosX < 0 || this.m_nDlgPosX > 3000)
			{
				this.m_nDlgPosX = 0;
			}
			if (this.m_nDlgPosY < 0 || this.m_nDlgPosY > 3000)
			{
				this.m_nDlgPosY = 0;
			}
			base.Location = new System.Drawing.Point(this.m_nDlgPosX, this.m_nDlgPosY);
		}

		private void btnSymbol1_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("°");
		}

		private void btnSymbol2_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("±");
		}

		private void btnSymbol3_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("φ");
		}

		private void btnSymbol4_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("%");
		}

		private void btnLexicon_Click(object sender, EventArgs e)
		{
			DlgDictForm dlgDictForm = new DlgDictForm();
			if (dlgDictForm.ShowDialog(this) == DialogResult.OK)
			{
				this.txtArea.Text = dlgDictForm.text;
			}
			this.SetFocusWithTextBox();
		}

		private void DlgPracticeDim_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgPracticeDim.SendMessage(CmdPracticeDim.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void txtArea_TextChanged(object sender, EventArgs e)
		{
			this.textStrArray = this.txtArea.Lines;
		}

		private void cboFontType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.sFontType = this.cboFontType.Text;
		}

		private void rdoLineEndText_CheckedChanged(object sender, EventArgs e)
		{
			this.bTextOnLine = false;
		}

		private void rdoLineUptext_CheckedChanged(object sender, EventArgs e)
		{
			this.bTextOnLine = true;
		}

		private void btnRebarSymbol1_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC00");
			this.AddTextToTextBox(str);
		}

		private void btnRebarSymbol2_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC01");
			this.AddTextToTextBox(str);
		}

		private void btnRebarSymbol3_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC02");
			this.AddTextToTextBox(str);
		}

		private void btnRebarSymbol4_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC03");
			this.AddTextToTextBox(str);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelCommand()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgPracticeDim.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgPracticeDim.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DlgPracticeDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			int wMsg = 7;
			DlgPracticeDim.SendMessage(CmdPracticeDim.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			System.Drawing.Point location = base.Location;
			this.m_nDlgPosX = location.X;
			this.m_nDlgPosY = location.Y;
			this.GetDlgData();
			this.WriteDlgData();
			this.CancelCommand();
		}

		private void AddTextToTextBox(string str)
		{
			int selectionStart = this.txtArea.SelectionStart;
			if (selectionStart >= 0)
			{
				this.txtArea.Text = this.txtArea.Text.Insert(selectionStart, str);
				this.txtArea.Focus();
				this.txtArea.SelectionStart = selectionStart + str.Length;
				return;
			}
			System.Windows.Forms.TextBox textBox = this.txtArea;
			textBox.Text += str;
			this.txtArea.Focus();
			this.txtArea.SelectionStart = selectionStart + str.Length;
		}

		private void SetFocusWithTextBox()
		{
			this.txtArea.Focus();
			this.txtArea.SelectionStart = this.txtArea.Text.Length;
		}

		private bool ReadDlgData()
		{
			string path = Path.GetTempPath() + "PracticeDimForm.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_nDlgPosX = Convert.ToInt32(streamReader.ReadLine());
				this.m_nDlgPosY = Convert.ToInt32(streamReader.ReadLine());
				this.bTextOnLine = Convert.ToBoolean(streamReader.ReadLine());
				this.bMutliDotOnLine = Convert.ToBoolean(streamReader.ReadLine());
				this.bSetLeaderAngle = Convert.ToBoolean(streamReader.ReadLine());
				this.sFontType = streamReader.ReadLine();
				this.sLeaderAngle = streamReader.ReadLine();
				this.sArrowStyle = streamReader.ReadLine();
				int num = Convert.ToInt32(streamReader.ReadLine());
				this.textStrArray = new string[num];
				for (int i = 0; i < num; i++)
				{
					this.textStrArray[i] = streamReader.ReadLine();
				}
			}
			catch
			{
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void WriteDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "PracticeDimForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_nDlgPosX);
			streamWriter.WriteLine(this.m_nDlgPosY);
			streamWriter.WriteLine(this.bTextOnLine);
			streamWriter.WriteLine(this.bMutliDotOnLine);
			streamWriter.WriteLine(this.bSetLeaderAngle);
			streamWriter.WriteLine(this.sFontType);
			streamWriter.WriteLine(this.sLeaderAngle);
			streamWriter.WriteLine(this.sArrowStyle);
			streamWriter.WriteLine(this.textStrArray.Length.ToString());
			foreach (string value in this.textStrArray)
			{
				streamWriter.WriteLine(value);
			}
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void SetDlgData()
		{
			this.rdoLineEndText.Checked = !this.bTextOnLine;
			this.rdoLineUptext.Checked = this.bTextOnLine;
			this.chkMultiDotOnLine.Checked = this.bMutliDotOnLine;
			this.chkSetLeaderAngle.Checked = this.bSetLeaderAngle;
			this.cmbLeaderLineAngle.Enabled = this.bSetLeaderAngle;
			this.txtArea.Lines = this.textStrArray;
			if (this.sFontType != null && this.cboFontType.Items.Contains(this.sFontType))
			{
				this.cboFontType.Text = this.sFontType;
			}
			else
			{
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				if (Convert.ToInt32(this.m_Revit.Application.Application.VersionNumber) > 2016)
				{
					list.Add("HY_仿宋_3.0mm");
					list.Add("HY_仿宋_3mm");
					list.Add("仿宋_3.0mm");
					list.Add("仿宋_3mm");
					list2.Add("_仿宋_");
					list2.Add("仿宋");
					list2.Add("_3.0mm");
					list2.Add("_3mm");
				}
				else
				{
					list.Add("HY_仿宋_3.5mm");
					list.Add("仿宋_3.5mm");
					list2.Add("_仿宋_");
					list2.Add("仿宋");
					list2.Add("_3.5mm");
				}
				string fortText = RevitDimensionUtiltyMethod.GetFortText(DimDate.FontFamily, list, list2);
				if (fortText != null && fortText != string.Empty)
				{
					this.cboFontType.Text = fortText;
				}
				else
				{
					this.cboFontType.SelectedIndex = 0;
				}
			}
			if (this.sArrowStyle != null && this.cmbArrowStyle.Items.Contains(this.sArrowStyle))
			{
				this.cmbArrowStyle.Text = this.sArrowStyle;
			}
			else
			{
				this.cmbArrowStyle.SelectedIndex = 0;
			}
			if (this.sLeaderAngle != null && this.cmbLeaderLineAngle.Items.Contains(this.sLeaderAngle))
			{
				this.cmbLeaderLineAngle.Text = this.sLeaderAngle;
				return;
			}
			this.cmbLeaderLineAngle.SelectedIndex = 0;
		}

		public void GetDlgData()
		{
			this.bTextOnLine = this.rdoLineUptext.Checked;
			this.bMutliDotOnLine = this.chkMultiDotOnLine.Checked;
			this.bSetLeaderAngle = this.chkSetLeaderAngle.Checked;
			this.sFontType = this.cboFontType.Text;
			this.sLeaderAngle = this.cmbLeaderLineAngle.Text;
			this.sArrowStyle = this.cmbArrowStyle.Text;
			this.textStrArray = this.txtArea.Lines;
		}

		private void cmbLeaderLineAngle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.sLeaderAngle = this.cmbLeaderLineAngle.Text;
		}

		private void cmbLeaderLineAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			Helper.TxtSetNum(sender, e);
		}

		private void chkMultiDotOnLine_CheckedChanged(object sender, EventArgs e)
		{
			this.bMutliDotOnLine = this.chkMultiDotOnLine.Checked;
		}

		private void chkSetLeaderAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.bSetLeaderAngle = this.chkSetLeaderAngle.Checked;
			this.cmbLeaderLineAngle.Enabled = this.bSetLeaderAngle;
		}

		private void cmbArrowStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.sArrowStyle = this.cmbArrowStyle.Text;
		}

		private void DlgPracticeDim_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private ExternalCommandData m_Revit;

		private int m_nDlgPosX;

		private int m_nDlgPosY;

		public bool bMutliDotOnLine;

		public bool bSetLeaderAngle;

		public bool bTextOnLine = true;

		public string sTextarea;

		public string sFontType = "宋体 5mm";

		public string sLeaderAngle = "0";

		public string sArrowStyle = "实心点 3mm";

		public string[] textStrArray;
	}
}
